package com.cxy521.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.cxy521.common.annotation.Log;
import com.cxy521.common.core.controller.BaseController;
import com.cxy521.common.core.domain.AjaxResult;
import com.cxy521.common.enums.BusinessType;
import com.cxy521.system.domain.CxySites;
import com.cxy521.system.service.ICxySitesService;
import com.cxy521.common.utils.poi.ExcelUtil;

/**
 * 站点信息Controller
 * 
 * @author zzs
 * @date 2025-01-09
 */
@RestController
@RequestMapping("/system/sites")
@Api(tags={"【站点信息】Controller"})
public class CxySitesController extends BaseController
{
    @Autowired
    private ICxySitesService cxySitesService;

    /**
     * 查询站点信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:sites:list')")
    @GetMapping("/list")
    @ApiOperation("查询站点信息列表")
    public AjaxResult list(CxySites cxySites)
    {
        List<CxySites> list = cxySitesService.selectCxySitesList(cxySites);
        return success(list);
    }

    /**
     * 导出站点信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:sites:export')")
    @Log(title = "站点信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("导出站点信息列表Excel")
    public void export(HttpServletResponse response, CxySites cxySites)
    {
        List<CxySites> list = cxySitesService.selectCxySitesList(cxySites);
        ExcelUtil<CxySites> util = new ExcelUtil<CxySites>(CxySites.class);
        util.exportExcel(response, list, "站点信息数据");
    }

    /**
     * 获取站点信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:sites:query')")
    @GetMapping(value = "/{siteId}")
    @ApiOperation("获取站点信息详细信息")
    public AjaxResult getInfo(@PathVariable("siteId") Long siteId)
    {
        return success(cxySitesService.selectCxySitesBySiteId(siteId));
    }

    /**
     * 新增站点信息
     */
    @PreAuthorize("@ss.hasPermi('system:sites:add')")
    @Log(title = "站点信息", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增站点信息")
    public AjaxResult add(@RequestBody CxySites cxySites)
    {
        cxySites.setCreateBy(getUsername());
        return toAjax(cxySitesService.insertCxySites(cxySites));
    }

    /**
     * 修改站点信息
     */
    @PreAuthorize("@ss.hasPermi('system:sites:edit')")
    @Log(title = "站点信息", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改站点信息")
    public AjaxResult edit(@RequestBody CxySites cxySites)
    {
        cxySites.setUpdateBy(getUsername());
        return toAjax(cxySitesService.updateCxySites(cxySites));
    }

    /**
     * 删除站点信息
     */
    @PreAuthorize("@ss.hasPermi('system:sites:remove')")
    @Log(title = "站点信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{siteId}")
	@ApiOperation("删除站点信息")
    public AjaxResult remove(@PathVariable Long siteId)
    {
        if (cxySitesService.hasChildBySiteId(siteId))
        {
            return warn("存在子站点,不允许删除");
        }
        return toAjax(cxySitesService.deleteCxySitesBySiteId(siteId));
    }
}
